package com.zjl.hiring.talent.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zjl.hiring.common.DBObject;
import com.zjl.hiring.common.util.DBUtil;
import com.zjl.hiring.talent.entity.ResumeEntity;

public class ResumeDao {

	public static List<ResumeEntity> list(int talentId) {
		List<ResumeEntity> resumeList = new ArrayList<>();
		String sql = "SELECT * FROM resume WHERE talent_id=?";
		DBObject dbObject = null;
		ResultSet resultSet = null;
		try {
			dbObject = DBUtil.executeQuery(sql, talentId);
			resultSet = dbObject.getResultSet();
			while (resultSet.next()) {
				ResumeEntity resume = new ResumeEntity();
				resume.setId(resultSet.getInt("id"));
				resume.setIntention(resultSet.getString("intention"));
				resume.setWorkExperience(resultSet.getString("work_experience"));
				resume.setProjectExperience(resultSet
						.getString("project_experience"));
				resumeList.add(resume);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return resumeList;
	}

	/**
	 * 简历的删除
	 * 
	 * @param resumeId
	 * @return
	 */
	public int deleteResume(String resumeId) {
		int row = 0;
		String sql = "DELETE FROM resume WHERE id=?";
		try {
			row = DBUtil.executeUpdate(sql, resumeId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 新增简历
	 */
	public int add(ResumeEntity resume) {
		int row = 0;
		String sql = "INSERT INTO resume VALUES(NULL ,? ,? ,? ,?)";
		List<Object> paramList = new ArrayList<>();
		paramList.add(resume.getTalentId());
		paramList.add(resume.getIntention());
		paramList.add(resume.getWorkExperience());
		paramList.add(resume.getProjectExperience());
		try {
			row = DBUtil.executeUpdate(sql, paramList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 更具Id查询简历信息
	 * 
	 * @return
	 */
	public ResumeEntity get(int resumeId) {
		ResumeEntity resume = null;
		DBObject dbObject = null;
		ResultSet resultSet = null;
		String sql = "SELECT * FROM resume WHERE id=?";
		try {
			dbObject = DBUtil.executeQuery(sql, resumeId);
			resultSet = dbObject.getResultSet();
			if (resultSet.next()) {
				resume = new ResumeEntity();
				resume.setId(resultSet.getInt("id"));
				resume.setIntention(resultSet.getString("intention"));
				resume.setWorkExperience(resultSet.getString("work_experience"));
				resume.setProjectExperience(resultSet
						.getString("project_experience"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			resume = null;
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return resume;
	}
	// 简历修改
	public int update(ResumeEntity resume) {
		int row = 0;
		String sql = "UPDATE resume SET intention=?, work_experience=?, project_experience=? WHERE id=?";
		try {
			List<Object> paramList = new ArrayList<>();
			paramList.add(resume.getIntention());
			paramList.add(resume.getWorkExperience());
			paramList.add(resume.getProjectExperience());
			paramList.add(resume.getId());
			row = DBUtil.executeUpdate(sql, paramList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return row;
	}

}
